package com.twosnail.frame.controller;

import com.jfinal.core.Controller;
import com.jfinal.ext.render.excel.PoiRender;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.Render;
import com.twosnail.frame.commin.util.result.ResultObj;
import com.twosnail.frame.model.CouponUseLog;
import com.twosnail.frame.model.SysUser;
import com.twosnail.frame.model.XdCoupon;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2017/7/11/011.
 */
public class CouponUseLogController extends Controller {

    private Logger logger = Logger.getLogger(CouponUseLogController.class) ;

    @RequiresPermissions("CouponUseLogController")
    public void index(){
        try {
            Integer pageNum = getParaToInt( "pageNum" ) ;
            Integer numPerPage = getParaToInt( "numPerPage" ) ;
            pageNum = pageNum == null ? 1 : pageNum;
            numPerPage = ( numPerPage == null || numPerPage == 0 ) ? 5 : numPerPage;
            Page<Record> list = CouponUseLog.me.getCouponUseLogs( getSession() , pageNum, numPerPage );
            setAttr( "list", list );
            setAttr( "use", CouponUseLog.me.getUse(getSession()));
            setAttr( "all", CouponUseLog.me.getAll(getSession()));
            setAttr( "neckName", getSessionAttr( "neckName" ) ) ;
        } catch (Exception e) {
            this.logger.warn( "用户列表信息，初始化失败！" , e );
        }
        render( "uselog_list.html" );
    }

    /**
     * 验证次卡
     */
    @RequiresPermissions("CouponUseLogController.check")
    public void check(){
        String checkCode  = getPara( "checkCode" ) ;
        try {
            if( checkCode == null || "".equals( checkCode ) ) {
                renderJson( new ResultObj( ResultObj.FAIL , "验证码不能为空！" , null ) );
            } else {
                CouponUseLog couponUseLog = CouponUseLog.me.getByCheckCode(checkCode,getSession());
                if( couponUseLog == null ){
                    renderJson( new ResultObj( ResultObj.FAIL , "未验证通过" , null ) );
                }else{
                    // 代金劵购买的健身劵 设置为已经使用
                    if( couponUseLog.getLong("xdCouponId") != null && couponUseLog.getLong("xdCouponId") > 0 ){
                        XdCoupon.me.updateState1(couponUseLog.getLong("xdCouponId"));
                    }
                    CouponUseLog.me.updateUseLog(couponUseLog,getSession());
                    renderJson( new ResultObj( ResultObj.SUCCESS , "验证通过" , null ) );
                }
            }
        } catch ( Exception e ) {
            this.logger.info( "系统异常，验证次卡失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }

    /**
     * 导出
     */
    @RequiresPermissions("CouponUseLogController.export")
    public void export() {
        List<Record> list = CouponUseLog.me.getListById(getSession());
        String[] header={"流水号","验证时间","验证码","门店名"};
        String[] columns={"orderCode","useTime","checkCode","neckName"};
        //其实关键代码就这一句话fileName、sheetName、columns、headers为必须
        Render poirender = PoiRender.me(list).fileName(System.currentTimeMillis()+".xls").headers(header).sheetName("健身劵使用记录").columns(columns);
        render(poirender);
    }
}
